﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Activity
{
    public class SearchActivityModel
    {
        [DisplayName("Buscar por Nombre")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras mayusculas o minusculas o numeros")]
        public string Pattern { get; set; }

        public List<SearchActivityInfo> ActivityInfo
        {
            get
            {
                return activityInfo;
            }
        }

        private List<SearchActivityInfo> activityInfo;

        public SearchActivityModel()
        {
            SearchArea();
        }

        public void SearchActivity(String pattern)
        {
            riskanalysisEntities entities = new riskanalysisEntities();
            var managements = from a in entities.activities
                              where a.name.Contains(pattern) && a.enabled
                              select new
                              {
                                  ActivityId = a.activityId,
                                  Name = a.name,
                                  Process = a.area.process.name,
                                  Area = a.area.name
                              };
            activityInfo = new List<SearchActivityInfo>();

            foreach (var man in managements)
            {
                activityInfo.Add(
                    new SearchActivityInfo
                    {
                        ActivityId = man.ActivityId,
                        Name = man.Name,                        
                        Process = man.Process,
                        Area = man.Area
                    });
            }  
            
        }

        public void SearchArea()
        {
            SearchActivity("");
        }
    }
}